package ssm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import ssm.dao.MyResult;
import ssm.dao.User;
import ssm.service.UserService;
import ssm.utils.Const;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;

    /**
     * 用户登录接口
     * @param username 用户账号
     * @param password 用户密码
     * @param session session会话
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public MyResult login(String username, String password, HttpSession session){
       return userService.login(username,password,session);
    }

    /**
     * 用户退出接口
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/logout",method = RequestMethod.GET)
    public MyResult logout(HttpSession session){
        session.removeAttribute(Const.CURRENT_USER);
        return MyResult.ok("成功退出");
    }


    /**
     * 注册用户接口
     * @param user
     * @return
     */
    @RequestMapping(value="/register",method = RequestMethod.POST)
    @ResponseBody
    public MyResult register(User user){
        MyResult response=userService.register(user);
        return response;
    }

    /**
     * 注册校验接口
     * @param str 校验内容
     * @param type 校验字段类型
     * @return
     */
    @RequestMapping(value="checkValid",method = RequestMethod.POST)
    @ResponseBody
    public MyResult checkValid( String str, String type){
        MyResult response=userService.checkValid(str,type);
        return response;
    }

    /**
     * 用户信息获取接口
     * @param session
     * @return
     */
    @RequestMapping(value="get_user_info",method = RequestMethod.POST)
    @ResponseBody
    public MyResult  getUserInfo(HttpSession session){
        User user=(User) session.getAttribute(Const.CURRENT_USER);
        if(user!=null){
            return MyResult.ok(user);
        }
        return MyResult.except("用户未登录,无法获取用户信息");
    }

    /**
     * 密码找回-获取问题接口
     * @param username
     * @return
     */
    @RequestMapping(value="forget_quest",method = RequestMethod.POST)
    @ResponseBody
    public MyResult forgetQuest(String username){
        return userService.selectGetQuest(username);
    }

    /**
     * 密码找回-核对密码获取token接口
     * @param username
     * @param question
     * @param answer
     * @return
     */
    @RequestMapping(value="forget_answer",method = RequestMethod.POST)
    @ResponseBody
    public MyResult forgetAnswer(String username,String question,String answer){
        return userService.forgetAnswer(username,question,answer);
    }

    /**
         *密码找回-修改密码接口
     * @param username
     * @param newPassword
     * @param token
     * @return
     */
    @RequestMapping(value="forget_restPassword",method = RequestMethod.POST)
    @ResponseBody
    public MyResult forgetRestPassword(String username,String newPassword,String token){
        return userService.forgetRestPassword(username,newPassword,token);
    }

    /**
     * 新旧密码核对接口
     * @param username
     * @param newPassword
     * @return
     */
    @RequestMapping(value="check_password_repeat",method = RequestMethod.POST)
    @ResponseBody
    public MyResult checkPasswordRepeat(String username,String newPassword){
        return userService.checkPasswordRepeat(username,newPassword);
    }

    /**
     * 在线修改密码接口
     * @param session
     * @param oldPassword
     * @param newPassword
     * @return
     */
    @RequestMapping(value="reset_password",method = RequestMethod.POST)
    @ResponseBody
    public MyResult reset_password(HttpSession session,String oldPassword,String newPassword){
        User user= (User) session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return MyResult.except("用户尚未登录");
        }
        return userService.resetPassword(user,oldPassword,newPassword);
    }

    /**
     * 在线更新个人信息
     * @param user
     * @param session
     * @return
     */
    @RequestMapping(value="update_infomation",method = RequestMethod.POST)
    @ResponseBody
    public MyResult update_information(User user,HttpSession session) {

        User currentUser = (User) session.getAttribute(Const.CURRENT_USER);
        if (currentUser == null) {
            return MyResult.except("用户尚未登录");
        }
        MyResult result= userService.update_information(user);
        if(result.getStatus()==200){
            session.setAttribute(Const.CURRENT_USER,result.getData());
        }
        return  result;
    }


    /**
     * 获取用户信息
     * @param session
     * @return
     */
    @RequestMapping(value="get_infomation.do",method = RequestMethod.POST)
    @ResponseBody
    public MyResult get_information(HttpSession session){
        User current_user=(User)session.getAttribute(Const.CURRENT_USER);
        if(current_user==null){
            return MyResult.UnLoginExcept();
        }
        MyResult result= userService.getInformation(current_user.getId());
        if (result.getStatus()==200){
            session.setAttribute(Const.CURRENT_USER,result.getData());
        }
        return result;
    }


}
